home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-10-16 | 11.0 KB | 378 lines | [TEXT/PJMM] |
- (*--------------------------------------------------------------------------*)
- (* *)
- (* CopybitsSpeedPalette. *)
- (* by John Wang *)
- (* *)
- (* Description: This program demostrates ways to increase copybits *)
- (* speed when using palettes. *)
- (* *)
- (* Version: 1.0 Completed 11/12/91 *)
- (* *)
- (*--------------------------------------------------------------------------*)
-
- {Think Pascal version by Ingemar 1994.}
- {• Pascal conversion.}
- {• Added two "draw" commands to draw several times with or without copying ctSeed. This makes the}
- {purpose of the demo *far* more obvious!}
- {• In other cases (updates, drag etc) when it used to do the demo drawing, it just draws once as a normal update.}
- {• Better zooming. The default now is the size where CopyBits is fastest.}
-
- {The conclusion of the demo: For top speed, use matching color tables and copy the ctSeed from the}
- {screen device to the offscreens.}
-
- program CopybitsSpeedPalette;
-
- uses
- Palettes, QDOffscreen;
-
- const
- kGestalttest = $A1AD;
- kNoTrap = $A89F;
-
- appleID = 128;
- appleMenu = 0;
- aboutMeCommand = 1;
-
- fileID = 129;
- drawNormalCommand = 1;
- drawFastCommand = 2;
- quitCommand = 4;
-
- pictID = 128;
- clutID = 150;
-
- aboutMeDLOG = 128;
- okButton = 1;
-
- (*------------------------------------------------------*)
- (* Global Variables. *)
- (*------------------------------------------------------*)
-
- var
- totalRect, minRect, winMinusScroll, initWindowSize: Rect;
- myWindow: WindowPtr;
- mycolors: CTabHandle;
- srcPalette: PaletteHandle;
- gDoneFlag: Boolean;
- mymenu0, mymenu1: MenuHandle;
- thePict: PicHandle;
- offscreenGWorld: GWorldPtr;
-
- (*------------------------------------------------------*)
- (* showAboutMeDialog(). *)
- (*------------------------------------------------------*)
-
- procedure ShowAboutMeDialog;
- var
- savePort: GrafPtr;
- theDialog: DialogPtr;
- itemHit: Integer;
- begin
- GetPort(savePort);
- theDialog := GetNewDialog(aboutMeDLOG, nil, WindowPtr(-1));
- SetPort(theDialog);
-
- repeat
- ModalDialog(nil, itemHit);
- until itemHit = okButton;
-
- CloseDialog(theDialog);
-
- SetPort(savePort);
- end;
-
- (*------------------------------------------------------*)
- (* init(). *)
- (*------------------------------------------------------*)
-
- procedure Init;
- var
- tempRgn: RgnHandle;
- baseRect: Rect;
- err: OSErr;
- QDfeature, OSfeature: LongInt;
- saveGD: GDHandle;
- savePort: CGrafPtr;
- begin
- (* Initialize Managers. *)
- {$IFC UNDEFINED THINK_PASCAL}
- InitGraf(qd.thePort);
- FlushEvents(everyEvent, 0);
- InitWindows;
- InitDialogs(nil);
- {$ENDC}
- InitCursor;
-
- (* Set up menus. *)
- mymenu0 := GetMenu(appleID);
- AddResMenu(mymenu0, 'DRVR');
- InsertMenu(mymenu0, 0);
- mymenu1 := GetMenu(fileID);
- InsertMenu(mymenu1, 0);
- DrawMenuBar;
- gDoneFlag := FALSE;
- thePict := GetPicture(pictID);
- if (thePict = nil) then
- gDoneFlag := TRUE;
-
- (* Use Gestalt to find if 32-bit QuickDraw is available. *)
- if ((GetTrapAddress(kGestalttest) <> GetTrapAddress(kNoTrap))) then
- begin
- err := Gestalt(gestaltQuickdrawVersion, QDfeature);
- if err <> noErr then
- gDoneFlag := TRUE;
- err := Gestalt(gestaltSystemVersion, OSfeature);
- if err <> noErr then
- gDoneFlag := TRUE;
- if not gDoneFlag then
- if (BitAnd(QDfeature, $0f00) <> $0200) and (OSfeature < $0605) then
- gDoneFlag := TRUE;
- end
- else
- gDoneFlag := TRUE;
-
- (* Set Rects. *)
- SetRect(baseRect, 40, 60, 472, 282);
- SetRect(winMinusScroll, baseRect.left - 40, baseRect.top - 60, baseRect.right - 60, baseRect.bottom - 80);
- {SetRect(initWindowSize, winMinusScroll.left, winMinusScroll.top, winMinusScroll.right, winMinusScroll.bottom);}
- initWindowSize := winMinusScroll;
- tempRgn := GetGrayRgn;
- HLock(Handle(tempRgn));
- totalRect := tempRgn^^.rgnBBox;
- SetRect(minRect, 80, 80, tempRgn^^.rgnBBox.right - 40, tempRgn^^.rgnBBox.bottom - 40);
- HUnlock(Handle(tempRgn));
-
- (* Open window and set up picture. *)
- GetGWorld(savePort, saveGD);
- mycolors := GetCTable(clutID);
- mycolors^^.ctFlags := BitOr(mycolors^^.ctFlags, $4000);
-
- myWindow := NewCWindow(nil, baseRect, '', TRUE, zoomDocProc, WindowPtr(-1), TRUE, 150);
- SetGWorld(CGrafPtr(myWindow), saveGD);
- DrawGrowIcon(myWindow);
-
- srcPalette := NewPalette((mycolors^^.ctSize) + 1, mycolors, pmTolerant + pmExplicit + pmAnimated, 0);
- SetPalette(myWindow, srcPalette, TRUE);
-
- GetGWorld(savePort, saveGD);
- err := NewGWorld(offscreenGWorld, 8, initWindowSize, mycolors, nil, []);
- if err <> noErr then
- begin
- SysBeep(1);
- halt;{Debugger;}
- end;{Probably out of memory}
- SetGWorld(offscreenGWorld, nil);
- EraseRect(initWindowSize);
- DrawPicture(thePict, initWindowSize);
- SetGWorld(savePort, saveGD);
- end;
-
- (*------------------------------------------------------*)
- (* draw. *)
- (*------------------------------------------------------*)
-
- procedure Draw (fast: Boolean);
- var
- { black, white: RGBColor;}
- before, delay: LongInt;
- theString: Str255;
- screensDevice: GDHandle;
- area: Rect;
- saveSeed: Longint;
- begin
- { black.red := 0;}
- { black.green := 0;}
- { black.blue := 0;}
- { white.red := -1;}
- { white.green := -1;}
- { white.blue := -1;}
- { }
- {RGBForeColor(black);}
- {RGBBackColor(White);}
-
- ForeColor(blackColor);
- BackColor(whiteColor);
-
- if fast then
- begin (* This is the only change made to support a faster copybits on one screen.}
- {ctFlags is still set above. *)
- area := winMinusScroll;
- LocalToGlobal(area.topLeft);
- LocalToGlobal(area.botRight);
- screensDevice := GetMaxDevice(area);
- saveSeed := offscreenGWorld^.portPixMap^^.pmTable^^.ctSeed;
- if screensDevice <> nil then
- offscreenGWorld^.portPixMap^^.pmTable^^.ctSeed := screensDevice^^.gdPMap^^.pmTable^^.ctSeed;
- end;
-
- before := TickCount;
-
- EraseRect(winMinusScroll);
- CopyBits(GrafPtr(offscreenGWorld)^.portBits, myWindow^.portBits, initWindowSize, winMinusScroll, srcCopy, nil);
- EraseRect(winMinusScroll);
- CopyBits(GrafPtr(offscreenGWorld)^.portBits, myWindow^.portBits, initWindowSize, winMinusScroll, srcCopy, nil);
- EraseRect(winMinusScroll);
- CopyBits(GrafPtr(offscreenGWorld)^.portBits, myWindow^.portBits, initWindowSize, winMinusScroll, srcCopy, nil);
- EraseRect(winMinusScroll);
- CopyBits(GrafPtr(offscreenGWorld)^.portBits, myWindow^.portBits, initWindowSize, winMinusScroll, srcCopy, nil);
- EraseRect(winMinusScroll);
- CopyBits(GrafPtr(offscreenGWorld)^.portBits, myWindow^.portBits, initWindowSize, winMinusScroll, srcCopy, nil);
- delay := TickCount - before;
-
- {Erase the space where the text goes.}
- SetPort(myWindow);
- area := myWindow^.portRect;
- area.top := area.bottom - 14;
- area.right := area.right - 15;
- EraseRect(area);
- {Now tell us how fast it was.}
- MoveTo(5, myWindow^.portRect.bottom - 3);
- DrawString(stringof('5 EraseRect+CopyBits in ', delay : 0, ' ticks.'));
-
- if fast then
- offscreenGWorld^.portPixMap^^.pmTable^^.ctSeed := saveSeed;
- end;
-
- (*------------------------------------------------------*)
- (* doCommand(). *)
- (*------------------------------------------------------*)
-
- procedure DoCommand (mResult: Longint);
- var
- theMenu, theItem: Integer;
- daName: Str255;
- savePort: GrafPtr;
- begin
- theItem := LoWord(mResult);
- theMenu := HiWord(mResult);
-
- case theMenu of
- appleID:
- if theItem = aboutMeCommand then
- ShowAboutMeDialog
- else
- begin
- GetItem(mymenu0, theItem, daName);
- GetPort(savePort);
- if OpenDeskAcc(daName) <> noErr then
- ;
- SetPort(savePort);
- end;
- fileID:
- case theItem of
- drawNormalCommand:
- Draw(false);
- drawFastCommand:
- Draw(true);
- quitCommand:
- gDoneFlag := TRUE;
- otherwise
- end; {case theItem}
- end;{case}
- HiliteMenu(0);
- end; {DoCommand}
-
- (*------------------------------------------------------*)
- (* main(). *)
- (*------------------------------------------------------*)
-
- var
- key: char;
- track: Boolean;
- growResult: LongInt;
- myEvent: EventRecord;
- whichWindow: WindowPtr;
- yieldTime: Integer;
-
- {main}
- begin
- Init;
- yieldTime := 0;
- while true do
- begin
- if (gDoneFlag) then
- ExitToShell;
-
- if WaitNextEvent(everyEvent, myEvent, yieldTime, nil) then
- case myEvent.what of
- mouseDown:
- case FindWindow(myEvent.where, whichWindow) of
- inSysWindow:
- SystemClick(myEvent, whichWindow);
- inMenuBar:
- DoCommand(MenuSelect(myEvent.where));
- inContent:
- ;
- inDrag:
- begin
- DragWindow(whichWindow, myEvent.where, totalRect);
- {Draw;}
- DrawGrowIcon(whichWindow);
- end;
- inGrow:
- begin
- growResult := GrowWindow(whichWindow, myEvent.where, minRect);
- SizeWindow(whichWindow, LoWord(growResult), HiWord(growResult), TRUE);
- EraseRect(whichWindow^.portRect);
- InvalRect(whichWindow^.portRect);
- SetRect(winMinusScroll, whichWindow^.portRect.left, whichWindow^.portRect.top, whichWindow^.portRect.right - 20, whichWindow^.portRect.bottom - 20);
- DrawGrowIcon(whichWindow);
- end;
- inGoAway:
- if TrackGoAway(whichWindow, myEvent.where) then
- begin
- CloseWindow(whichWindow);
- gDoneFlag := TRUE;
- end;
- inZoomIn:
- if TrackBox(whichWindow, myEvent.where, inZoomIn) then
- begin
- ZoomWindow(whichWindow, inZoomIn, TRUE);
- EraseRect(whichWindow^.portRect);
- InvalRect(whichWindow^.portRect);
- SetRect(winMinusScroll, whichWindow^.portRect.left, whichWindow^.portRect.top, whichWindow^.portRect.right - 20, whichWindow^.portRect.bottom - 20);
- DrawGrowIcon(whichWindow);
- end;
- inZoomOut:
- if TrackBox(whichWindow, myEvent.where, inZoomOut) then
- begin
- SizeWindow(whichWindow, initWindowSize.right - initWindowSize.left + 20, initWindowSize.bottom - initWindowSize.top + 20, true);
- {ZoomWindow(whichWindow, inZoomOut, TRUE);}
- EraseRect(whichWindow^.portRect);
- InvalRect(whichWindow^.portRect);
- SetRect(winMinusScroll, whichWindow^.portRect.left, whichWindow^.portRect.top, whichWindow^.portRect.right - 20, whichWindow^.portRect.bottom - 20);
- DrawGrowIcon(whichWindow);
- end;
- otherwise
- end; {case FindWindow}
- keyDown, autoKey:
- begin
- key := Char(BitAnd(myEvent.message, charCodeMask));
- if BitAnd(myEvent.modifiers, cmdKey) <> 0 then
- if (myEvent.what = keyDown) then
- DoCommand(MenuKey(key));
- end;
- updateEvt:
- if WindowPtr(myEvent.message) = myWindow then
- begin
- BeginUpdate(myWindow);
- CopyBits(GrafPtr(offscreenGWorld)^.portBits, myWindow^.portBits, initWindowSize, winMinusScroll, srcCopy, nil);
- DrawGrowIcon(myWindow);
- EndUpdate(myWindow);
- end;
- diskEvt:
- ;
- activateEvt:
- ;
- app4Evt:
- if (BSL(myEvent.message, 31) = 0) then
- yieldTime := 30
- else
- begin
- yieldTime := 0;
- SetPort(myWindow);
- end;
- otherwise
- end; {case}
- end; {while true}
- end.